<?php

class SlideshowController extends MyController
{
	function init()
	{
		parent::init();
	}
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}	
	
	function actionView($page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->website["beperpage"];
		
		$rows = slideshow::model()->findAll(array(
			"order" => "`order`",
			"offset" => ($page-1) * $pages->pageSize,
			"limit" => $pages->pageSize
		));
		
		$pages->itemCount =  slideshow::model()->count(); // tong so dong
			
		$this->data['rows']= $rows;
		
		$this->data["pages"] = $pages;
		$this->data["page"] = $page;
		$this->render('view', $this->data);
	}
	

	function actionAdd($page = 1)
	{
		$row = new slideshow;
		$row->id = 0;
		$this->data["row"] = $row;
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($slideshow_id = 0, $page = 1)
	{
		$row = slideshow::model()->findByPk($slideshow_id);
		$this->data["row"] = $row;
		
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionSave($slideshow_id = 0,  $page = 1)
	{
		
		$row = NULL;
		if ($slideshow_id == 0)
		{
			$row = new slideshow;
			$row->name = Yii::app()->getRequest()->getPost("name");
			$row->url = Yii::app()->getRequest()->getPost("url");
			$row->order = Yii::app()->getRequest()->getPost("order");
			$row->save();
			
			//Upload bg_picture
			$types = array(
				'image/pjpeg' => 'jpg',
				'image/jpeg' => 'jpg',
				'image/x-png' => 'png',
				'image/png' => 'png',
				'image/gif' => 'gif'
			);
			
			$file = CUploadedFile::getInstanceByName("file");
			
			if(!is_null($file))
			{
				
				$file->saveAs(WEB_ROOT."/slides/slide_".$row->id.".".$types[$file->type]);
				$row->image = "slide_".$row->id.".".$types[$file->type];
				$row->save();
			}
			
			
		}
		else
		{
			$row = slideshow::model()->findByPk($slideshow_id);
			$row->name = Yii::app()->getRequest()->getPost("name");
			$row->url = Yii::app()->getRequest()->getPost("url");
			$row->order = Yii::app()->getRequest()->getPost("order");
			
			//Upload bg_picture
			$types = array(
				'image/pjpeg' => 'jpg',
				'image/jpeg' => 'jpg',
				'image/x-png' => 'png',
				'image/png' => 'png',
				'image/gif' => 'gif'
			);
			
			$file = CUploadedFile::getInstanceByName("file");
			
			if(!is_null($file))
			{
				
				$file->saveAs(WEB_ROOT."/slides/slide_".$row->id.".".$types[$file->type]);
				$row->image = "slide_".$row->id.".".$types[$file->type];
			}
			
			$row->save();
		}
		
		$this->redirect(array($this->controller_id.'/view', "page"=>$page));
	}
	
	
	function actionEnable($slideshow_id = 0, $page = 1)
	{
		if ($slideshow_id == 0)
		{
			throw new CHttpException(404,'The specified post cannot be found.');
		}
		else
		{
			$row = slideshow::model()->findByPk($slideshow_id);
			$row->enable = 1 - $row->enable;
			$row->save();
			$this->redirect(array($this->data['controller_id'].'/view', "page"=>$page));
		}
	}
	
	function actionSet_order($page = 1)
	{
		//Tao thu tu mac dinh
		$rows = slideshow::model()->findAll();
		foreach($rows as $row)
		{
			$row->order = $row->id;
			$row->save();
		}
		$this->redirect(array($this->controller_id.'/view', 
			"parent_id"=>$parent_id, "page"=>$page));	
	}
	
	function actionSave_order($page = 0)
	{
		$orders = Yii::app()->getRequest()->getPost("order");
		
		foreach($orders as $id=>$value)
		{
			$row = slideshow::model()->findByPk($id);
			$row->order = $value;
			$row->save();
		}
		
		//print_r($orders);
		$this->redirect(array($this->controller_id.'/view', "page"=>$page));		
	}
	
	function actionDelete($slideshow_id, $page)
	{
		$row = slideshow::model()->findByPk($slideshow_id);
		@ unlink(WEB_ROOT."/slides/".$row->image);	
		slideshow::model()->deleteByPk($slideshow_id);
		
		$rows = slideshow::model()->findAll(array(
			"offset" => ($page-1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', "page"=>$page));
	}
	
	function actionDelete_multi($page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value){
				$row = slideshow::model()->findByPk($id);
				@ unlink(WEB_ROOT."/slides/".$row->image);
				slideshow::model()->deleteByPk($id);
			}
		}
		
		$rows = slideshow::model()->findAll(array(
			"offset" => ($page-1)*$this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', "page"=>$page));
	}
	
	function actionDelete_image($slideshow_id, $page )
	{
		$row = slideshow::model()->findByPk($slideshow_id);
		
		if (strlen($row->image)>0)
		{
			@ unlink(WEB_ROOT."/slides/".$row->image);
			$row->image ='';
			$row->save();
		}
		
		$this->redirect(
			array($this->controller_id.'/edit', 
			"slideshow_id" => $slideshow_id,
			"page"=>$page
		));
	}
}
